package com.rustic.server.controller.admapi;

import cn.xy.base.server.web.AdminWebContext;
import com.rustic.server.query.RusticAdminAdminListQuery;
import com.rustic.server.dto.RusticAdminAddDTO;
import com.rustic.server.dto.RusticAdminUpdateDTO;
import com.rustic.server.po.RusticAdmin;
import com.rustic.server.service.RusticAdminService;
import com.rustic.server.vo.list.RusticAdminAdminListVO;
import com.rustic.server.vo.RusticAdminVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import cn.xy.commons.annotation.OperateLog;
import cn.xy.commons.core.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 表[rustic_admin]对应的后台管理类
 *
 * @author auto
 */

@Slf4j
@RestController
@RequestMapping("/admapi/rusticAdmin")
@Api(tags = {"管理员乡村关联管理接口"})
public class RusticAdminAdminController {

    @Autowired
    private RusticAdminService rusticAdminService;

    @ApiOperation(value = "管理员乡村关联-列表", notes = "权限key: rustic-admin-list")
    @GetMapping("/list")
    @PreAuthorize("@validate.hasPermission('rustic-admin-list')")
    public Result<Page<RusticAdminAdminListVO>> list(@Validated RusticAdminAdminListQuery query) {
        return Result.data(rusticAdminService.getPageList(query));
    }

    @ApiOperation(value = "管理员乡村关联-详情", notes = "权限key: rustic-admin-detail")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "管理员乡村关联id"),
    })
    @GetMapping("/detail")
    @PreAuthorize("@validate.hasPermission('rustic-admin-detail')")
    public Result<RusticAdminVO> detail(int id) {
        if (id <= 0) {
            return Result.error(RusticAdminService.RUSTIC_ADMIN_NOT_EXIST);
        }
        RusticAdmin rusticAdmin = rusticAdminService.getById(id);
        if (rusticAdmin == null) {
            return Result.error(RusticAdminService.RUSTIC_ADMIN_NOT_EXIST);
        }
        RusticAdminVO rusticAdminVO = new RusticAdminVO(rusticAdmin);
        return Result.data(rusticAdminVO);
    }

    @ApiOperation(value = "管理员乡村关联-增加", notes = "权限key: rustic-admin-add")
    @PostMapping("/add")
    @PreAuthorize("@validate.hasPermission('rustic-admin-add')")
    @OperateLog(description = "增加管理员乡村关联")
    public Result<String> add(@RequestBody @Validated RusticAdminAddDTO rusticAdminAddDTO) {
        RusticAdmin rusticAdmin = rusticAdminAddDTO.toRusticAdmin();
        rusticAdminService.save(rusticAdmin);
        return Result.ok("添加管理员乡村关联成功");
    }

    @ApiOperation(value = "管理员乡村关联-更新", notes = "权限key: rustic-admin-update")
    @PutMapping("/update")
    @PreAuthorize("@validate.hasPermission('rustic-admin-update')")
    @OperateLog(description = "更新管理员乡村关联")
    public Result<String> update(@RequestBody @Validated RusticAdminUpdateDTO rusticAdminUpdateDTO) {
        RusticAdmin rusticAdmin = rusticAdminService.getById(rusticAdminUpdateDTO.getId());
        if (rusticAdmin == null) {
            return Result.error(RusticAdminService.RUSTIC_ADMIN_NOT_EXIST);
        }
        BeanUtils.copyProperties(rusticAdminUpdateDTO, rusticAdmin);
        rusticAdminService.updateById(rusticAdmin);
        return Result.ok("编辑管理员乡村关联成功");
    }

    @ApiOperation(value = "管理员乡村关联-删除", notes = "权限key: rustic-admin-delete")
    @DeleteMapping("/delete")
    @PreAuthorize("@validate.hasPermission('rustic-admin-delete')")
    @OperateLog(description = "删除管理员乡村关联")
    public Result<String> delete(int adminId, int rusticId) {
        if (adminId <= 0) {
            return Result.error(RusticAdminService.RUSTIC_ADMIN_NOT_EXIST);
        }
        RusticAdmin rusticAdmin = rusticAdminService.getByAdminIdAndRusticId(adminId, rusticId);
        if (rusticAdmin == null) {
            return Result.error(RusticAdminService.RUSTIC_ADMIN_NOT_EXIST);
        }
        rusticAdminService.deleteLink(adminId, rusticId, AdminWebContext.getAdminId());
        return Result.ok("删除管理员乡村关联成功");
    }
}
